package it.polimi.reTravelDream.beans;

import it.polimi.reTravelDream.dto.HotelDTO;
import it.polimi.reTravelDream.eao.HotelEAOLocal;

import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;




@ManagedBean(name="hotel")
@RequestScoped
public class HotelBean {
	
	private HotelDTO result;
	@EJB 
	private HotelEAOLocal hotelAction;
	
	private HotelDTO currentHotel;
	
	private String errorPrice;
	private String errorId;
	private String errorName;
	private String errorDescription;
	private String errorCity;
	private String errorAddress;
	
	private String confirmCreation;

	
	public HotelBean() {
		this.currentHotel=new HotelDTO(); 
	}

	public void addHotel() {
		if (!this.check()) {
			return;
		}
		try {
			this.currentHotel.setActive(true);
			this.hotelAction.addHotel(this.currentHotel);
			this.confirmCreation="hotel created!";
			this.currentHotel=new HotelDTO();
			
		} catch (Exception e) {
			
		}
		
		
	}
	
	private boolean check(){
		this.errorId="";
		this.errorAddress="";
		this.errorDescription="";
		this.errorName="";
		this.errorPrice="";
		
		this.confirmCreation="";
		
		boolean value=true;
		
		if (!this.checkId()) {
			this.errorId="id already present";
			value=false;
		}
		if (!this.checkPrice()) {
			this.errorPrice="price not valid";
			value=false;
		}
		if(!this.checkName()){
			
			value=false;
		}
		if (!this.checkDescription()) {
			this.setErrorDescription("the description can not be empty ");
			value=false;
		}
		if (!this.checkCity()) {
			this.errorCity="you must choose a city";
			value=false;
		}
		if (!this.checkAddress()) {
			this.errorAddress="address not valid";
			value=false;
		}
		return value;
	}
	
	private boolean checkPrice(){
			if(this.currentHotel.getPrice()==0){
				return false;
			}
		return true;
	}
	
	private boolean checkId(){	
		List<HotelDTO> allhoteList=this.hotelAction.getAllHotel();		
			for(HotelDTO hDto:allhoteList){
				if (this.currentHotel.getId()==hDto.getId()) {
					return false;			
				}
			}
		return true;
	}
	
	private boolean checkDescription(){
		if(this.currentHotel.getDescription().isEmpty()){
			return false;
		}
		return true;
	}
	
	private boolean checkName(){

		if (this.currentHotel.getName().isEmpty()) {
			this.errorName="the name can not be empty";
			return false;
		}
	return true;
	}
	
	private boolean checkCity(){
		if (this.currentHotel.getCity().isEmpty()) {
			return false;
		}
		return true;
	}
	
	private boolean checkAddress(){
		if(this.currentHotel.getAddress().isEmpty()){
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * getter and setter
	 */
	
	public HotelDTO getCurrentHotel() {
		return currentHotel;
	}

	public void setCurrentHotel(HotelDTO currentHotel) {
		this.currentHotel = currentHotel;
	}
	
	public void getAllHotels(){
		List<HotelDTO> hotels= hotelAction.getAllHotel();
		this.result=hotels.get(0);
	}

	public HotelDTO getResult() {
		return result;
	}

	public void setResult(HotelDTO result) {
		this.result = result;
	}

	public String getErrorPrice() {
		return errorPrice;
	}

	public void setErrorPrice(String errorPrice) {
		this.errorPrice = errorPrice;
	}

	public String getErrorId() {
		return errorId;
	}

	public void setErrorId(String errorId) {
		this.errorId = errorId;
	}

	public String getErrorName() {
		return errorName;
	}

	public void setErrorName(String errorName) {
		this.errorName = errorName;
	}

	public String getErrorDescription() {
		return errorDescription;
	}

	public void setErrorDescription(String errorDescription) {
		this.errorDescription = errorDescription;
	}

	public String getErrorCity() {
		return errorCity;
	}

	public void setErrorCity(String errorCity) {
		this.errorCity = errorCity;
	}

	public String getErrorAddress() {
		return errorAddress;
	}

	public void setErrorAddress(String errorAddress) {
		this.errorAddress = errorAddress;
	}

	public String getConfirmCreation() {
		return confirmCreation;
	}

	public void setConfirmCreation(String confirmCreation) {
		this.confirmCreation = confirmCreation;
	}


}